package com.betterjr.modules.risk.loan.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.risk.loan.entity.HttpConnectionResult;
import com.betterjr.modules.risk.loan.entity.TransactionDTO;
import com.betterjr.modules.risk.loan.field.ConfigFieldCollections;

public class HttpRequestTest {

    private static String prefixFundSystemUrl = "http://192.168.0.152:8090";

    public static void main(String[] args) {

        // 发送http请求查询交易数据
        Map<String, Object> map = QueryTermBuilder.newInstance().put("idList", "1f8689571bc94533b9b1ee5a35e03641")
                .build();
        HttpRequestObject requestObject = createRequest(map, ConfigFieldCollections.REQUEST_HTTP_METHOD_POST,
                "/api/trade/right");

        FutureUtil.getHttpFuture(requestObject);

        List<TransactionDTO> transactionLists = getFuture(requestObject).getTransactionLists();

        for (TransactionDTO transactionDTO : transactionLists) {
            System.out.println(transactionDTO);
        }

        // String tokin = getTokin();
    }

    public static String getTokin() {

        String string = HttpRequest.sendGet(
                "http://192.168.0.152:8090/oauth/token?grant_type=client_credentials&client_id=client_1&client_secret=123456&scope=all",
                "", null);
        System.out.println(string);

        return string;

    }

    private static HttpRequestObject createRequest(Map<String, Object> anParamMap, String requestType, String apiUrl) {

        return HttpRequestBuilder.newInstance().url(prefixFundSystemUrl + apiUrl)
                .paramObject(JSONObject.toJSONString(anParamMap)).requestType(requestType).builder();

    }

    private static HttpConnectionResult getFuture(HttpRequestObject requestObject) {

        HttpConnectionResult returnResult = HttpConnectionResult.ok();
        String result = FutureUtil.getHttpResult(requestObject);
        try {

            if (requestObject.getUrl()
                    .equals(prefixFundSystemUrl + ConfigFieldCollections.QUERY_BUSINESS_SINGLETON_API_BYPATH)) {

                // 查询接口
                returnResult.setTransactionLists(JSONObject.parseArray(result, TransactionDTO.class));
            } else if (requestObject.getUrl()
                    .equals(prefixFundSystemUrl + ConfigFieldCollections.SAVE_TRANSACTION_DATA_API_PATH)) {

                // 修改接口
                returnResult = JSONObject.parseObject(result, HttpConnectionResult.class);
            }
        }
        catch (Exception e) {

            BTAssert.notNull(null, "调用远程接口返回值异常" + result);
        }

        return returnResult;

    }

    @Test
    public void runtest() {

        Map<String, String> headMap = new HashMap<>();
        headMap.put("Authorization", "Bearer d354b873-fb54-4455-bb48-9cc78390915e");
        String string = HttpRequest.sendPost("http://192.168.0.152:8090/api/trade/detail",
                "[\"ac33c24f14994351aeefa5b71d9f4e18\"]", headMap);
        System.out.println(string);
    }

}
